package com.fsc.comadapoc1.application
{
	import com.fsc.comadapoc1.domain.PortfolioModel;
	import com.fsc.comadapoc1.domain.UserModel;
	import com.fsc.comadapoc1.entities.Portfolio;
	import com.fsc.comadapoc1.enums.GUID;
	import com.fsc.comadapoc1.events.LoadLiquidityExitFeesEvent;
	import com.fsc.comadapoc1.events.LoadPortfolioEvent;
	import com.fsc.comadapoc1.events.PortfolioSearchEvent;
	
	import flash.events.EventDispatcher;
	import flash.external.ExternalInterface;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.AsyncToken;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.remoting.RemoteObject;
	import mx.utils.ArrayUtil;
	
	[Event(name="loadLiquidityExitFees", type="com.fsc.comadapoc1.events.LoadLiquidityExitFeesEvent")]
	[Event(name="portfolioLoadComplete",type="com.fsc.comadapoc1.events.LoadPortfolioEvent")]
	[ManagedEvents(names="portfolioLoadComplete,loadLiquidityExitFees")]
	public class GetPortfolioPendingTradesCommand extends EventDispatcher
	{
	
	
		[Inject(id="portfolioService")]
		public var service:RemoteObject;
		
		/**
		 * Contains objects to manage portfolios
		 */ 
		[Inject]
		[Bindable]
		public var model:PortfolioModel;
		
		
		[Inject]
		[Bindable]
		public var userModel:UserModel;
		
		
		private var _portfolioID : String;
		private var inited:Boolean = false;
		
		
		
		public function init():void{
		
			inited = true;
		}
		
		
		public function execute(event:LoadPortfolioEvent):AsyncToken {
			if(!inited){
				init();
			}
			//set the current portfolio
			model.currentPortfolio = event.portfolio;
			model.currentPortfolio.pendingTrades = new ArrayCollection();
		
			dispatchEvent(new LoadLiquidityExitFeesEvent(LoadLiquidityExitFeesEvent.LOAD_LIQUIDITY_EXIT_FEES));
			return service.GetPortfolioPendingTrades( userModel.guid,model.currentPortfolio.name,true,true,model.currentPortfolio.pendingTradesPage ) as AsyncToken;
		}
		
		/******************************************
		 *  Result handler 
		 */
		public function result(data:Object):void
		{
			var loadPortfolioComplete:LoadPortfolioEvent = new LoadPortfolioEvent(LoadPortfolioEvent.LOAD_COMPLETE);
			
			if ( data.Results != null)
			{
				trace("Setting the pending results");
				var arr:Array;
				arr = ArrayUtil.toArray(data.Results)
				for each (var obj:Object in arr )
				{
					if( obj.Id == model.currentPortfolio.id ){
						var tradesArr:Array;
						tradesArr = ArrayUtil.toArray(obj.Trades)
						for each (var objt:Object in tradesArr )
						{
							model.currentPortfolio.pendingTrades.addItem(objt)		
						}
					}
						
					
				}
			}
			else
			{
				trace("SearchCommand: Clearing Results");
			}
			dispatchEvent(loadPortfolioComplete);
			model.searchResults.enableAutoUpdate();
		}
		
		/******************************************
		 *  Fault handler in case the request failed. 
		 */
		public function error (fault:FaultEvent, trigger:LoadPortfolioEvent) : void {
			//need to add some logging here
			Alert.show(fault.fault.message,fault.fault.faultDetail);
		}  
	}
		
}